<?php

// +----------------------------------------------------------------------
// |Author: 翼小菜
// +----------------------------------------------------------------------
// |Description: 
// +----------------------------------------------------------------------
// |更多功能可联系QQ314688769
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// |Author: 翼小菜
// +----------------------------------------------------------------------
// |Description: 模版数据模型
// +----------------------------------------------------------------------
// |更多功能可联系QQ314688769
// +----------------------------------------------------------------------
namespace api\cveadmin\model;
use think\Model;
class ThemeFileModel extends Model
{
    protected $name = 'theme_file';
    protected $type = [
        'more'        => 'array',
        'config_more' => 'array',
        'draft_more'  => 'array'
    ];

    public function fillBlockWidgetValue($blockName, $widgetId)
    {
        $oldMore = $this['more'];
        $widget  = [];
        if (isset($oldMore['widgets_blocks'][$blockName]['widgets'][$widgetId])) {
            $widgetWithValue = $oldMore['widgets_blocks'][$blockName]['widgets'][$widgetId];
            $theme           = $this['theme'];
            $widgetManifest  = file_get_contents(WEB_ROOT . "themes/$theme/public/widgets/{$widgetWithValue['name']}/manifest.json");
            $widget          = json_decode($widgetManifest, true);

            foreach ($widgetWithValue as $key => $value) {
                if ($key == 'vars') {
                    foreach ($value as $varName => $varValue) {
                        if (isset($widget['vars'][$varName])) {
                            $widget['vars'][$varName]['value'] = $varValue;
                        }
                    }
                } else {
                    $widget[$key] = $value;
                }
            }

        }

        return $widget;
    }
}
